﻿using System;
using System.Net;
using System.Text;
using Golos.Helper;
using Golos.Http;
using Golos.Users.UserCache;

namespace Golos.Users.Http
{
    public partial class UserCacheServer : HttpServer
    {
        public UserCacheServer(int port) : base(port)
        {
            userCache = new Cache();
            LogClass.WriteInfoEntry(string.Format("Listening on http://*:{0}/", port));
        }

        protected override void IncomingRequest(HttpListenerContext context, ref int StatusCode)
        {
            string method = context.Request.HttpMethod;
            switch (method.ToUpperInvariant())
            {
                case "GET":
                    Get(context, ref StatusCode);
                    break;
                case "POST":
                    Post(context, ref StatusCode);
                    break;
                default:
                    break;
            }
        }

        private void Post(HttpListenerContext context, ref int statusCode)
        {
            string operation = context.Request.QueryString["operation"];
            switch (operation)
            {
                case "register":
                    RegisterUser(context, ref statusCode);
                    break;
                case "attach":
                    AttachUser(context, ref statusCode);
                    break;
                case "updateprofile":
                    UpdateProfile(context, ref statusCode);
                    break;
                default :
                    break;
            }
        }

        private void Get(HttpListenerContext context, ref int statusCode)
        {
            string operation = context.Request.QueryString["operation"];
            switch (operation)
            {
                case "userinfo":
                    GetUserInfo(context, ref statusCode);
                    break;
                default:
                    break;
            }
        }

        private Cache userCache;
    }
}
